import { getBaseUrl } from '@/services/urlconst';
import { getTenantID, requestFunComponFiex } from '@/utils';
import { message } from 'antd';
import { useMemo } from 'react';
import { useRequest } from 'umi';

let baseUrl = getBaseUrl();

/**
 * 获取新增流程相关数据
 * @returns
 */
const getFlowRelevantService = () => {
  const getFlowRelevant = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/getFlowRelevant`;
      let bodyParams = {
        ...params,
        orderType: `${getTenantID()}1002`,
        flowType: `${getTenantID()}9903`,
        checkType: `${getTenantID()}9902`,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        if (!(res && res?.msgFlag)) {
          message.error(res?.msg);
        }
      },
    },
  );
  // private String id;//主键
  // private String name;//类型名称
  // private String flowType;//流程类型(查询时可以选择多个,逗号隔开)
  // private String flowTypeName;//流程类型名称(可以通过名字查询)
  // private String tableName;//表名(英文 大小写字母和下划线)
  // private String stateType;//业务状态
  // private String stateTypeName;//业务状态名称
  // private String checkType;//审核类型
  // private String checkTypeName;//审核类型名称
  // private String modelTypeIds;//批量操作使用
  // private Integer seq;//排序
  // private Integer versionNum; //版本号

  const data = useMemo(() => {
    return getFlowRelevant?.data?.obj;
  }, [getFlowRelevant?.data]);

  return {
    getFlowRelevantRun: getFlowRelevant?.run,
    getFlowRelevantLoading: getFlowRelevant?.loading,
    getFlowRelevantData: data,
  };
};

/**
 * 新增流程类别
 * @param modelTypeAddCallback
 * @returns
 */
const modelTypeAddService = (modelTypeAddCallback: (res: any) => void) => {
  const modelTypeAdd = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/modelTypeAdd`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: modelTypeAddCallback,
    },
  );

  return {
    modelTypeAddRun: modelTypeAdd?.run,
    modelTypeAddLoading: modelTypeAdd?.loading,
  };
};

/**
 * 编辑流程类别
 * @param modelTypeEditCallback
 * @returns
 */
const modelTypeEditService = (modelTypeEditCallback: (res: any) => void) => {
  const modelTypeEdit = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/modelTypeEdit`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('PUT', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: modelTypeEditCallback,
    },
  );

  return {
    modelTypeEditRun: modelTypeEdit?.run,
    modelTypeEditLoading: modelTypeEdit?.loading,
  };
};

/**
 * 新增流程模型
 * @param modelAddCallback
 * @returns
 */
const modelAddService = (modelAddCallback: (res: any) => void) => {
  const modelAdd = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/modelAdd`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: modelAddCallback,
    },
  );

  return {
    modelAddRun: modelAdd?.run,
    modelAddLoading: modelAdd?.loading,
  };
};

/**
 * 编辑流程模型
 * @param modelEditCallback
 * @returns
 */
const modelEditService = (modelEditCallback: (res: any) => void) => {
  const modelEdit = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/modelEdit`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('PUT', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: modelEditCallback,
    },
  );

  return {
    modelEditRun: modelEdit?.run,
    modelEditLoading: modelEdit?.loading,
  };
};

/**
 * 发布流程模型
 * @param modelDeployCallback
 * @returns
 */
const modelDeployService = (modelDeployCallback: (res: any) => void) => {
  const modelDeploy = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/activitiController/modelDeploy`;
      let bodyParams = {
        ...params,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: modelDeployCallback,
    },
  );

  return {
    modelDeployRun: modelDeploy?.run,
    modelDeployLoading: modelDeploy?.loading,
  };
};

export {
  getFlowRelevantService,
  modelTypeAddService,
  modelTypeEditService,
  modelAddService,
  modelEditService,
  modelDeployService,
};
